package shop.xmall.system.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import shop.xmall.system.domain.SysRole;
import shop.xmall.system.domain.SysUserRole;
import shop.xmall.system.mapper.SysRoleMapper;
import shop.xmall.system.mapper.SysUserRoleMapper;
import shop.xmall.system.service.ISysRoleService;
import shop.xmall.utils.DateUtils;
import shop.xmall.utils.text.Convert;

import java.util.List;

/**
 * 角色信息Service业务层处理
 *
 * @author xmall
 * @date 2019-10-09
 */
@Service
public class SysRoleServiceImpl implements ISysRoleService {
    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    /**
     * 查询角色信息
     *
     * @param roleId 角色信息ID
     * @return 角色信息
     */
    @Override
    public SysRole selectSysRoleById(Long roleId) {
        return sysRoleMapper.selectSysRoleById(roleId);
    }

    /**
     * 查询角色信息列表
     *
     * @param sysRole 角色信息
     * @return 角色信息
     */
    @Override
    public List<SysRole> selectSysRoleList(SysRole sysRole) {
        return sysRoleMapper.selectSysRoleList(sysRole);
    }

    /**
     * 新增角色信息
     *
     * @param sysRole 角色信息
     * @return 结果
     */
    @Override
    public int insertSysRole(SysRole sysRole) {
        sysRole.setCreateTime(DateUtils.getNowDate());
        return sysRoleMapper.insertSysRole(sysRole);
    }

    /**
     * 修改角色信息
     *
     * @param sysRole 角色信息
     * @return 结果
     */
    @Override
    public int updateSysRole(SysRole sysRole) {
        sysRole.setUpdateTime(DateUtils.getNowDate());
        return sysRoleMapper.updateSysRole(sysRole);
    }

    /**
     * 删除角色信息对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    public int deleteSysRoleByIds(String ids) {
        return sysRoleMapper.deleteSysRoleByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除角色信息信息
     *
     * @param roleId 角色信息ID
     * @return 结果
     */
    public int deleteSysRoleById(Long roleId) {
        return sysRoleMapper.deleteSysRoleById(roleId);
    }

    /**
     * 根据用户id查询角色信息
     */
    public List<SysRole> listByUserId(Long userId) {
        SysRole sysRole = new SysRole();
        sysRole.setStatus("0");
        List<SysRole> sysRoleList = selectSysRoleList(sysRole);
        if (sysRoleList != null && sysRoleList.size() > 0) {
            if (userId != null) {
                SysUserRole sysUserRole = new SysUserRole();
                sysUserRole.setUserId(userId);
                List<SysUserRole> sysUserRoleList = sysUserRoleMapper.selectListByUserRole(sysUserRole);
                for(SysRole role : sysRoleList){
                    for(SysUserRole userRole : sysUserRoleList){
                        if(role.getRoleId().equals(userRole.getRoleId())){
                            role.setUserChecked(true);
                        }
                    }
                }
            }
        }
        return sysRoleList;
    }

    @Override
    public int isExist(SysRole sysRole) {
        return sysRoleMapper.isExist(sysRole);
    }
}